package com.xnck.example.jertest.dao.helper;

import org.nutz.dao.Condition;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Entity;
import org.nutz.dao.entity.Record;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.springframework.beans.factory.annotation.Autowired;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * 只读实体的BaseDao
 */
public class BaseRODao {

    @Autowired
    private Dao dao;

    private Dao getDao() {
        return dao;
    }

    private void setDao(Dao dao) {
        this.dao = dao;
    }

    protected Sql getSql(String sqlName){
        return this.getDao().sqls().create(sqlName);
    }

    protected int queryCount(Sql sql){
        sql.setCallback(new SqlCallback() {
            public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
                int count = -1;
                if(rs.next()){
                    count = rs.getInt(1);
                }
                return count;
            }
        });
        this.getDao().execute(sql);
        return sql.getInt();
    }

    protected int queryCount(Sql sql, Condition condition){
        sql.setCondition(condition);
        return this.queryCount(sql);
    }

    protected List<Map> queryMaps(Sql sql){
        sql.setCallback(Sqls.callback.maps());
        this.getDao().execute(sql);
        return sql.getList(Map.class);
    }

    protected List<Map> queryMaps(Sql sql,  int currentPage, int pageSize){
        Pager pager = this.getDao().createPager(currentPage, pageSize);
        sql.setPager(pager);
        return this.queryMaps(sql);
    }

    protected List<Map> queryMaps(Sql sql, Condition condition){
        sql.setCondition(condition);
        return this.queryMaps(sql);
    }

    protected List<Map> queryMaps(Sql sql, Condition condition, int currentPage, int pageSize){
        Pager pager = this.getDao().createPager(currentPage, pageSize);
        sql.setPager(pager);
        sql.setCondition(condition);
        return this.queryMaps(sql);
    }

    protected List<Record> queryRecords(Sql sql){
        sql.setCallback(Sqls.callback.records());
        this.getDao().execute(sql);
        return sql.getList(Record.class);
    }

    protected List<Record> queryRecords(Sql sql, int currentPage, int pageSize){
        Pager pager = this.getDao().createPager(currentPage, pageSize);
        sql.setPager(pager);
        sql.setCallback(Sqls.callback.records());
        this.getDao().execute(sql);
        return sql.getList(Record.class);
    }

    protected List<Record> queryRecords(Sql sql, Condition condition, int currentPage, int pageSize){
        Pager pager = this.getDao().createPager(currentPage, pageSize);
        sql.setPager(pager);
        sql.setCondition(condition);
        sql.setCallback(Sqls.callback.records());
        this.getDao().execute(sql);
        return sql.getList(Record.class);
    }

    protected <T> List<T> search(Class<T> tClass, String sqlName){
        Sql sql = this.getDao().sqls().create(sqlName);
        return this.search(tClass, sql);
    }

    protected <T> List<T> search(Class<T> tClass, String sqlName, int currentPage, int pageSize){
        Sql sql = this.getDao().sqls().create(sqlName);
        return this.search(tClass, sql, currentPage, pageSize);
    }

    protected <T> List<T> search(Class<T> tClass, String sqlName, Condition condition, int currentPage, int pageSize){
        Sql sql = this.getDao().sqls().create(sqlName);
        return this.search(tClass, sql, condition, currentPage, pageSize);
    }

    protected <T> List<T> search(Class<T> tClass, Sql sql, int currentPage, int pageSize){
        Pager pager = this.getDao().createPager(currentPage, pageSize);
        sql.setPager(pager);
        return this.search(tClass, sql);
    }

    protected <T> List<T> search(Class<T> tClass, Sql sql, Condition condition, int currentPage, int pageSize){
        Pager pager = this.getDao().createPager(currentPage, pageSize);
        sql.setPager(pager);
        sql.setCondition(condition);
        return this.search(tClass, sql);
    }

    protected <T> List<T> search(Class<T> tClass, Sql sql){
        sql.setCallback(Sqls.callback.entities());
        Entity<T> entity = dao.getEntity(tClass);
        sql.setEntity(entity);
        dao.execute(sql);
        return sql.getList(tClass);
    }
}
